/*
 * Copyright 2013 Krzysztof Smigielski
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nilcaream.dirstore.cli;

import com.google.inject.AbstractModule;
import com.google.inject.multibindings.Multibinder;
import com.google.inject.name.Names;
import com.nilcaream.dirstore.cli.command.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TODO
 * <p/>
 * Krzysztof Smigielski 3/27/13 8:28 PM
 */
public class CliModule extends AbstractModule {

    private Configuration configuration;

    public CliModule(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    protected void configure() {
        setUpLog4j();

        bind(Configuration.class).toInstance(configuration);
        bind(Logger.class).toInstance(LoggerFactory.getLogger("cli"));

        bind(Integer.class).annotatedWith(Names.named("maxImageSize")).toInstance(configuration.getMaxImageSize());
        bind(Integer.class).annotatedWith(Names.named("maxFileSize")).toInstance(configuration.getMaxFileSize());

        Multibinder<Command> commandBinder = Multibinder.newSetBinder(binder(), Command.class);
        commandBinder.addBinding().to(ShowHelp.class);
        commandBinder.addBinding().to(TrimDirectory.class);
        commandBinder.addBinding().to(TrimZip.class);
        commandBinder.addBinding().to(FindDuplicates.class);
    }

    private void setUpLog4j() {
        String log4jFileName;
        if (configuration.isVerboseOutput()) {
            log4jFileName = "log4j-verbose.xml";
        } else {
            log4jFileName = "log4j.xml";
        }
        if (System.getProperty("log4j.configuration") == null) {
            System.setProperty("log4j.configuration", log4jFileName);
        }
    }
}
